package com.ocr.camera;

import com.ocr.utils.BingTranslate;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class TranslateAc extends Activity implements OnItemSelectedListener,
		OnClickListener {

	static String[] FROM_LANG = { "Vietnamese", "English" };
	static String[] TO_LANG = { "Vietnamese", "English", "Japanese" };

	Spinner from_lang;
	Spinner to_lang;
	TextView origin_text;
	TextView translate_text;
	Button translate_button;
	String translate;
	Button clean;
	ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.translate);

		// get view
		from_lang = (Spinner) findViewById(R.id.from_lang);
		to_lang = (Spinner) findViewById(R.id.to_lang);
		origin_text = (TextView) findViewById(R.id.origin);
		translate_text = (TextView) findViewById(R.id.translate);
		translate_button = (Button) findViewById(R.id.translate_button);
		clean = (Button) findViewById(R.id.clean);
		// set action
		from_lang.setOnItemSelectedListener(this);
		to_lang.setOnItemSelectedListener(this);
		translate_button.setOnClickListener(this);
		clean.setOnClickListener(this);

		// from language spinner
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.from_lang, android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		from_lang.setAdapter(adapter);

		// to language spinner
		adapter = ArrayAdapter.createFromResource(this, R.array.to_lang,
				android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		to_lang.setAdapter(adapter);
		
		Bundle bun = getIntent().getExtras();
		String text = bun.getString("text");
		origin_text.setText(text);
	}
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			dialog.dismiss();
			// view result
			translate_text.setText(translate);
		}
	};
	/*
	 * translate
	 */
	public void translate() {
		String from = from_lang.getSelectedItem().toString();
		String to = to_lang.getSelectedItem().toString();
		String origin_character = origin_text.getText().toString();

		// bing translate
		translate = BingTranslate.translate(origin_character, from, to);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		// translate();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view == translate_button) {
			dialog = ProgressDialog.show(this, "Translating",
					"Please, wait for few minute ...");

			new Thread() {
				public void run() {
					translate();
					handler.sendEmptyMessage(0);
				}
			}.start();

		}else if(view == clean){
			origin_text.setText("");
		}
	}
}
